#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class VoteItemProvider : CY.GFive.Core.Providers.DALProvider.IVoteItemProvider
	{
		#region Sql
		private static readonly string SqlSelectVoteItem = "SELECT [Id], [VoteItemContent],[VoteID] FROM [VoteItem] WHERE  [Id]=@Id";

		private static readonly string SqlInsertVoteItem = "INSERT INTO [VoteItem]([VoteItemContent],[VoteID]) values(@VoteItemContent,@VoteID);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateVoteItem = "UPDATE [VoteItem] SET [VoteItemContent] = @VoteItemContent,[VoteID] = @VoteID WHERE  [Id]=@Id";

		private static readonly string SqlDeleteVoteItem = "DELETE [VoteItem] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllVoteItem = "SELECT [Id], [VoteItemContent],[VoteID] FROM [VoteItem]";
		#endregion

		#region IVoteItemProvider Members
		public Core.Business.VoteItem Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectVoteItem);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.VoteItem voteItem = new Core.Business.VoteItem();

				if (!reader.IsDBNull(0)) voteItem.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) voteItem.VoteItemContent = reader.GetString(1);
				if (!reader.IsDBNull(2)) voteItem.VoteID = reader.GetInt32(2);

				return voteItem;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.VoteItem voteItem)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@VoteItemContent", SqlDbType.NVarChar, voteItem.VoteItemContent);
			sql.AddParameter("@VoteID", SqlDbType.Int, voteItem.VoteID);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertVoteItem);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) voteItem.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.VoteItem voteItem)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@VoteItemContent", SqlDbType.NVarChar, voteItem.VoteItemContent);
			sql.AddParameter("@VoteID", SqlDbType.Int, voteItem.VoteID);
			sql.AddParameter("@Id", SqlDbType.Int, voteItem.Id);
			sql.ExecuteSql(SqlUpdateVoteItem);
		}

		public void Delete(Core.Business.VoteItem voteItem)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, voteItem.Id);
			sql.ExecuteSql(SqlDeleteVoteItem);
		}

		public IList<Core.Business.VoteItem> GetAllVoteItem()
		{
			IList<Core.Business.VoteItem> voteItemlist = new List<Core.Business.VoteItem>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllVoteItem);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.VoteItem voteItem = new Core.Business.VoteItem();

					if (!reader.IsDBNull(0)) voteItem.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) voteItem.VoteItemContent = reader.GetString(1);
					if (!reader.IsDBNull(2)) voteItem.VoteID = reader.GetInt32(2);

					voteItem.MarkOld();
					voteItemlist.Add(voteItem);
				}
				reader.Close();
			}
			return voteItemlist;
		}
		#endregion
	}
}
